package com.rh.gtoa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rh.gtoa.common.model.PageResult;
import com.rh.gtoa.common.model.Result;
import com.rh.gtoa.model.UmsUserRole;
import com.rh.gtoa.service.IUmsUserRoleService;
import com.rh.gtoa.util.EasyPoiExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户角色关联表
 *
 * @author hqb
 * @date 2020-09-12 16:31:21
 */
@Slf4j
@CrossOrigin
@RestController
@Api(tags = "用户角色关联表")
@RequestMapping("/ums")
public class UmsUserRoleController {
    @Autowired
    private IUmsUserRoleService umsUserRoleService;

    /**
     * 列表
     */
    @ApiOperation(value = "查询列表")
    @PostMapping("/umsUserRole/list")
    public Result<PageResult> list(@RequestBody Map<String, Object> params) {
        Page<UmsUserRole> list= umsUserRoleService.findList(params);
        return Result.succeed(PageResult.restPage(list),"查询成功");
    }

    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @PostMapping("/umsUserRole/sel/{id}")
    public Result findUserById(@PathVariable Long id) {
        UmsUserRole model = umsUserRoleService.getById(id);
        return Result.succeed(model, "查询成功");
    }

    /**
     * 新增or更新
     */
    @ApiOperation(value = "保存")
    @PostMapping("/umsUserRole/save")
    public Result save(@RequestBody UmsUserRole umsUserRole) {
        umsUserRoleService.saveOrUpdate(umsUserRole);
        return Result.succeed("保存成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @PostMapping("/umsUserRole/del/{id}")
    public Result delete(@PathVariable Long id) {
        umsUserRoleService.removeById(id);
        return Result.succeed("删除成功");
    }
    
    /**
     * 导入
     */
    @ApiOperation(value = "导入")
    @PostMapping("/umsUserRole/leadIn")
    public  Result leadIn(MultipartFile excel, String cuId) throws Exception {
        int rowNum = 0;
        if (!excel.isEmpty()) {
            List<UmsUserRole> list = EasyPoiExcelUtil.importExcel(excel, 1, 1, UmsUserRole.class);
            rowNum = list.size();
            if (rowNum > 0) {
                //无该用户信息
                list.forEach(u -> {
                        umsUserRoleService.save(u);
                });
                return Result.succeed("成功导入信息"+rowNum+"行数据");
            }
        }
        return Result.failed("导入失败");
    }
    
    /**
     * 导出
     */
    @ApiOperation(value = "导出")
    @PostMapping("/umsUserRole/leadOut")
    public void leadOut(String cuId, HttpServletResponse response) throws IOException {
        List<UmsUserRole> umsUserRoles =new ArrayList<>();
        List<UmsUserRole> umsUserRoleList = umsUserRoleService.list(new QueryWrapper<UmsUserRole>().eq("cu_id", cuId));
        if (umsUserRoleList.isEmpty()) {umsUserRoles.add(umsUserRoleService.getById(0)); } else {
            for (UmsUserRole umsUserRole : umsUserRoleList) {
                umsUserRoles.add(umsUserRole);
            }
        }
        //导出操作
        EasyPoiExcelUtil.exportExcel(umsUserRoles, "用户角色关联表导出", "用户角色关联表导出", UmsUserRole.class, "umsUserRole.xls", response);

    }
    /**
     * 新增or更新
     */
    @ApiOperation(value = "未分配-》已分配")
    @PostMapping("/umsUserRole/saveUserRole")
    public Result saveUserRole(@RequestBody Map<String,List<UmsUserRole>> map) {
        List<UmsUserRole> userRoles = map.get("userRoles");
        umsUserRoleService.saveOrUpdateBatch(userRoles);
        return Result.succeed("保存成功");
    }
    @ApiOperation(value = "已分配-》未分配")
    @PostMapping("/umsUserRole/remove")
    public Result editUserRole(@RequestBody Map<String,List<UmsUserRole>> map) {
        List<UmsUserRole> userRoles = map.get("userRoles");
        for (UmsUserRole userRole : userRoles) {
            Long userId = userRole.getUserId();
            Long roleId = userRole.getRoleId();
            umsUserRoleService.remove(new QueryWrapper<UmsUserRole>()
                    .eq("user_id",userId)
                    .eq("role_id",roleId)
            );
        }
        return Result.succeed("移除成功");
    }
}
